#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QDateTime>
#include <QPainter>
#include <vector>

using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    _xpos = 0;

    QTimer* timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(GetRandom()));
    timer->start(1000);
    qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));

}

MainWindow::~MainWindow()
{
    delete ui;
}

int MainWindow::GetRandom()
{
    int rand = qrand() % 200;
    ui->lineEdit_2->setText(QString::number(rand, 10));
    _points.push_back(-rand);
    return rand;
}

void MainWindow::paintEvent(QPaintEvent*)
{
    int xpos = 0;
    int ypos = 0;
    int x = 0;
    QPainter paint( this) ;

    paint.translate(50,250);
    paint.drawLine(-30, 0, 500, 0);
    paint.drawLine(0, -250, 0, 30);

    vector<int>::iterator iter;
    if (_points.size() == 21)
    {
        _points.erase(_points.begin());
    }

    for (iter = _points.begin(); iter != _points.end(); ++iter)
    {
        int y = *iter;

        paint.drawLine(xpos, ypos, x, y);
        this->update();
        xpos = x;
        ypos = y;
        x += 20;
    }
}




